package com.yq.activemq.p2p;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;

public class AppConsumer {

    private static final String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    private static final String queueName = "queue-test";

    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(queueName);
        MessageConsumer consumer = session.createConsumer(queue);

        consumer.setMessageListener(message -> {
            try {
                String text = ((TextMessage) message).getText();
                System.out.println(text);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        });

        // 不能者连接是不能关闭的，如果关闭，会导致接收到不到mq中的消息。
    }
}
